package com.palm.hubo2o.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.palm.hubo2o.R;
import com.palm.hubo2o.adapter.BallGroupAdapter;
import com.palm.hubo2o.event.BetEvent;
import com.palm.hubo2o.model.Bet;
import com.palm.hubo2o.util.EnumCode;
import com.palm.hubo2o.view.GridItemDecoration;
import com.palm.hubo2o.view.K3BallView;
import com.palm.palmutils.LayoutUtils;
import com.palm.palmutils.widget.EasyRecyclerView;

import butterknife.Bind;
import de.greenrobot.event.EventBus;

/**
 * Created by dest1 on 2015/10/14.
 * 三不同号
 */
public class K3BT3Fragment extends LotteryFragment implements EasyRecyclerView.OnItemClickListener, EasyRecyclerView.CustomChoiceListener {
    @Bind(R.id.recycler_view)
    EasyRecyclerView mGrid;
    private GridLayoutManager manager;

    private final Bet bet = new Bet(EnumCode.gameId.HBP3);
    private String[] ballArray;

    @Override
    protected void onClearChoices() {
        mGrid.clearChoices();
        bet.getReds().clear();
    }

    @Override
    public boolean onRandomBalls() {
        return false;
    }


    @Override
    public void onCreateView2(View view, Bundle savedInstanceState) {
        mGrid.setChoiceMode(EasyRecyclerView.CHOICE_MODE_MULTIPLE_CUSTOM);
        manager = new GridLayoutManager(getContext(), 4);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int type = mGrid.getAdapter().getItemViewType(position);
                if (type == BallGroupAdapter.ITEM_TYPE.ITEM_TYPE_MARK.ordinal() || adapter.getItemGroupIndex(position) == 1)
                    return manager.getSpanCount();
                return 1;
            }
        });
//        mGrid.setSelector(RippleSalon.generateRippleDrawable(false));
        mGrid.setDrawSelectorOnTop(true);
        mGrid.addItemDecoration(new GridItemDecoration(LayoutUtils.dp2pix(getContext(), 2)));
        mGrid.setLayoutManager(manager);
        mGrid.setAdapter(adapter);
        mGrid.setCustomCheckedListener(this);
        mGrid.setOnItemClickListener(this);
        mGrid.intoCustomChoiceMode();//开启选中模式
        ballArray = getResources().getStringArray(R.array.K3_3bt_numbers);
        adapter.setBallNumber(ballArray.length, 1);
    }

    @Override
    public void onDestroyView() {
        /*bug of recyclerview and GridLayoutManager with SpanSizeLookup*/
        ((GridLayoutManager) mGrid.getLayoutManager()).setSpanSizeLookup(null);
        mGrid.setLayoutManager(null);
        mGrid.setAdapter(null);
        super.onDestroyView();
    }

    private BallGroupAdapter adapter = new BallGroupAdapter() {
        @Override
        public long getItemId(int position) {
            return super.getItemId(position);
        }

        @Override
        public BallHolder onCreateItemHolder(ViewGroup parent) {
            K3BallView ball = new K3BallView(parent.getContext());
            return new BallHolder(ball);
        }

        @Override
        public BallHolder onCreateLabelHolder(ViewGroup parent) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_k3_mark, parent, false);
            v.setEnabled(false);
            return new BallHolder(v);
        }

        @Override
        public void onBindItemViewHolder(BallHolder holder, int groupIndex, int position) {
            K3BallView ball = (K3BallView) holder.itemView;
            if (groupIndex == 0) {
                ball.setNumText(ballArray[position]);
            } else {
                ball.setNumText("三连号通选");
                ball.setAward("任意开出即中10元");
            }

        }


        @Override
        public void onBindMarkViewHolder(BallHolder holder, int groupIndex) {
            TextView textView = (TextView) holder.itemView.findViewById(R.id.title_mark);
            textView.setText(groupIndex == 0 ? "三不同号:奖金40元" : null);
        }


    };

    @Override
    public boolean onItemClick(EasyRecyclerView parent, View view, int position, long id) {
        parent.toggleItemChecked(position);
        return true;
    }


    @Override
    public void onIntoCustomChoice(EasyRecyclerView view) {

    }

    @Override
    public void onOutOfCustomChoice(EasyRecyclerView view) {

    }

    @Override
    public void onItemCheckedStateChanged(EasyRecyclerView view, int position, long id, boolean checked) {
        //球状态改变操作
        int index = adapter.getItemGroupIndex(position);
        if (index == 0) {
            K3BallView ballView = (K3BallView) view.findViewHolderForAdapterPosition(position).itemView;
            String ballText = ballView.getNumTex();
            if (checked) {
                if (bet.getReds().contains("")) {
                    Toast.makeText(getContext(), "三不同号不支持不同玩法投注", Toast.LENGTH_SHORT).show();
                    view.setItemChecked(position, false);
                } else {
                    bet.setPlayType(EnumCode.PlayType.K3_3BT);
                    bet.getReds().add(ballText);
                }
            } else {
                bet.getReds().remove(ballText);
            }
        } else {//三连号通选
            if (checked) {
                if (bet.getReds().size() != 0) {
                    Toast.makeText(getContext(), "三不同号不支持不同玩法投注", Toast.LENGTH_SHORT).show();
                    view.setItemChecked(position, false);
                } else {
                    bet.setPlayType(EnumCode.PlayType.K3_3LH);
                    bet.getReds().add("");
                }
            } else {
                bet.getReds().remove("");
            }
        }
        if (bet.getReds().size() > 0) {
            bet.setWagerCount(bet.getReds().size());
            EventBus.getDefault().post(new BetEvent(bet));
        } else {
            EventBus.getDefault().post(new BetEvent());
        }
    }


}
